<%@page import="News.dao.*"%>
<%@page import="java.sql.Timestamp"%>
<%@page import="News.entity.*,java.text.*"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>修改单条新闻处理页面</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">

</head>

<body>


	<%
		request.setCharacterEncoding("UTF-8");
		News n = new News();
		Date d = new Date();
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String st = sd.format(d);
		n.setNmodifyDate(st);
		DiskFileItemFactory df = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(df);
		upload.setHeaderEncoding("UTF-8");
		List<FileItem> list = upload.parseRequest(request);
		for (FileItem f : list) {
			if (f.isFormField()) {
				if (f.getFieldName().equalsIgnoreCase("nid")) {
					n.setNid(Integer.parseInt(f.getString("UTF-8")));
				}
				if (f.getFieldName().equalsIgnoreCase("ntid")) {
					n.setNtid(Integer.parseInt(f.getString("UTF-8")));
				}
				if (f.getFieldName().equalsIgnoreCase("ntitle")) {
					n.setNtitle(f.getString("UTF-8"));
				}
				if (f.getFieldName().equalsIgnoreCase("nauthor")) {
					n.setNauthor(f.getString("UTF-8"));
				}
				if (f.getFieldName().equalsIgnoreCase("nsummary")) {
					n.setNsummary(f.getString("UTF-8"));
				}
				if (f.getFieldName().equalsIgnoreCase("ncontent")) {
					n.setNcontent(f.getString("UTF-8"));
				}
			} else {
				String oldName = f.getName();
				if (oldName == null || oldName.trim().equals("")) {
					continue;
				}
				String filePath = application.getRealPath("images");
				String houzhui = oldName.substring(oldName.lastIndexOf('.'));
				if (!(houzhui.equals(".png") || houzhui.equals(".bmp") || houzhui.equals(".gif")
						|| houzhui.equals(".jpg"))) {
					out.print("<script>alert('你上传的不是图片文件！');history.back();</script>");
					return;
				}
				String newFileName = UUID.randomUUID() + houzhui;
				f.write(new java.io.File(filePath, newFileName));
				n.setNpicPath("images/" + newFileName);
			}
		}
		 Newsdao nd=new Newsdao();	
		int result = nd.update(n);
		if (result > 0) {
			out.print("<script>alert('新闻修改成功！');window.location.href='modifynews.jsp';</script>");
		} else {
			out.print("<script>alert('新闻修改失败！');history.back();</script>");
		}
	%>
</body>
</html>
